<?php
/**
 * Created by PhpStorm.
 * User: 姚鹏
 * Date: 2017-11-01
 * Time: 15:51
 */
namespace app\mall\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use app\mall\model\GoodsYongLiaoModel;

class Recipe extends BasicAdmin
{
	/**
	 * 配方管理
	 * @lingyun
	 * @return [type] [description]
     * 2018-03-16
	 */
	public function index(){
		$this->title = "配方管理";
		$table = 'Goods';
		$get = $this->request->get();
		$join = [
			['ys_goods b','a.goods_id=b.id','LEFT'],
		];
        $map = [];
        if(!empty($get['name'])){
            $map['b.name'] = array('like',"%".$get['name']."%");
        }
        $map['a.is_deleted'] = array('eq',0);
        $map['a.is_recipe'] = array('eq',1);        //是否有配方
        $map['b.is_deleted'] = array('eq',0);       //商品没有删除

		$db = Db::name('goods_spec_detail')->alias('a')->join($join)->where($map)->field('b.name,b.logo,a.title,a.goods_id,a.id');
		$yangshi = config("qiniu");
        $this->assign('yangshi',$yangshi['yangshi']);          //七牛样式后缀

        return parent::_list($db);
	}

    /**
     * @Author  lingyun
     * @Desc    添加商品配方
     * @return mixed
     * 2018-03-26
     */
	public function add_pecipe(){
		$table = "GoodsYongliao";
		if(request()->isPost()){
			$post = input('post.');
			list($goods_id,$goods_spec_id) = explode('|',$post['goods_id']);
			if(empty($goods_id)){
                $this->error('请选择商品','');
            }
			$data = $this->combibe_arr($post);
			$model = new GoodsYongLiaoModel();
			$result = $model->saveall($data);
			if($result>0){
				Db::name('goods_spec_detail')->where('id',$goods_spec_id)->update(['is_recipe'=>1]);
				$this->success('保存成功','');
			}else{
				$this->error('保存失败','');
			}
		}else{
		    //查询还没有分配配方的商品
            $goods = db('goods')->alias('a')->join('ys_goods_spec_detail b','a.id=b.goods_id','LEFT')->where(['a.is_deleted'=>0,'b.is_recipe'=>0,'b.is_deleted'=>0])->field('b.goods_id,b.id,concat_ws(" -- ",a.name,b.title) as subject')->select();

			empty($goods) && $this->error('暂无商品需要添加配方','');

			$material = Db::name('goods_material')->where('is_deleted',0)->field('id,name,unit,addtime')->select();
			$this->assign('material',$material);
			$this->assign('goods',$goods);

			return $this->fetch('add_pecipe');
		}
	}
	/**
	 * 详情 -- 编辑
	 * @Author   lingyun
	 * @DateTime 2017-11-30T16:10:13+0800
	 * @return   [type]                   [description]
     * 2018-03-26
	 */
	public function detail(){
		if(request()->isPost()){
			$post = input('post.');
            list($goods_id,$goods_spec_id) = explode('|',$post['goods_id']);
            if(empty($goods_id)){
                $this->error('请选择商品','');
            }
			$data = $this->combibe_arr($post);
			$model = new GoodsYongLiaoModel();
            $result = $model->saveall($data);
			if($result!==false){
				Db::name('goods')->where('id',$goods_id)->update(['is_peifang'=>1]);
				db('goods_spec_detail')->where('id',$goods_spec_id)->update(['is_recipe'=>1]);
				$this->success('保存成功','');
			}else{
				$this->error('保存失败','');
			}
		}else{
			$id = input('id');			//商品id和属性id组合  1|5	
			list($goods_id,$goods_spec_id) = explode('|',$id);
			$good = Db::name('goods')->where('id',$goods_id)->value('name');
			$spec = Db::name('goods_spec_detail')->where('id',$goods_spec_id)->value('title');
			$data = Db::name('goods_yongliao')->where(['goods_id'=>$goods_id,'goods_spec_id'=>$goods_spec_id,'is_deleted'=>0])->select();
			$material = Db::name('goods_material')->where('is_deleted',0)->select();

			$this->assign('data',$data);
			$this->assign('good',$good);
			$this->assign('material',$material);
			$this->assign('spec',$spec);
			$this->assign('goods_id',$id);

			return $this->fetch('detail');
		}
	}
	/**
	 * 删除配方的原料
	 * @lingyun
	 * @return [type] [description]
     * 2018-03-26
	 */
	public function del(){
		$id = input('id');
		$res = Db::name('goods_yongliao')->where('id',$id)->update(['is_deleted'=>1]);
		$res!==false?$this->success('删除成功',''):$this->error('删除失败','');
	}

    /**
     * @Author  lingyun
     * @Desc    删除配方
     * 2018-03-26
     */
    public function del_recipe(){
        $id = input('param.');		//接受的商品ID和属性ID的组合形式  goods_id|goods_spec_id
        list($goods_id,$goods_spec_id) = explode('|',$id['id']);
        $res = DB::name('goods_yongliao')->where(['goods_id'=>$goods_id,'goods_spec_id'=>$goods_spec_id])->update(['is_deleted'=>1]);
        db('goods_spec_detail')->where('id',$goods_spec_id)->update(['is_recipe'=>0]);
        $res>0?$this->success('删除成功',''):$this->error('删除失败','');
    }

	/**
	 * 组合配方数据
	 * @Author   lingyun
	 * @DateTime 2017-11-30T15:02:30+0800
	 * @param    [type]                   $data [description]
	 * @return   [type]                         [description]
     * 2018-03-26
	 */
	private function combibe_arr($data){
		list($goods_id,$goods_spec_id) = explode('|',$data['goods_id']);
		$num = $data['num'];						//原料数量
		$material_id = $data['material_id'];		//原料id
		foreach($material_id as $k => $v){
		    if(empty($v)){
		        $this->error('请选择原料','');
            }
            if(empty($num[$k])){
		        $this->error('请输入原料数量','');
            }
			if(isset($data['id'][$k])){
				$res[$k]['id'] = $data['id'][$k];		//主键ID
			}
			$res[$k]['material_id'] = $v;		//原料id
			$res[$k]['goods_id'] = $goods_id;	//商品id
			$res[$k]['num'] = $num[$k];			//原料数量
			$res[$k]['goods_spec_id'] = $goods_spec_id;
			$res[$k]['addtime'] = time();
		}
		return $res;
	}



}
?>